#导入模块
import cv2 as cv

def face_detect_demo(img):
    #将图片转换为灰度图片
    gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    #加载特征数据
    face_detector=cv.CascadeClassifier('data/haarcascade_frontalface_default.xml')
    faces=face_detector.detectMultiScale(gray)
    for x,y,w,h in faces:
        cv.rectangle(img,(x,y),(x+w,y+h),color=(0,255,0),thickness=2)
        # cv.circle(img,center=(x+w//2, y+h//2),radius=w//2, color=(0,255,255), thickness=2)
    cv.imshow('result',img)

#获取视频
cap=cv.VideoCapture('data/shaonaiyi.mp4')
while True:
    flag,frame=cap.read()
    print('flag:',flag,'frame.shape:',frame.shape)
    if not flag:
        break
    face_detect_demo(frame)
    #注意此处waitKey不能取0，否则页面就静止了
    if ord('q') == cv.waitKey(10):
        break

cv.destroyAllWindows()
cap.release()

